package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsReq;
import com.lkd.http.vo.TaskReq;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Topic(TopicConfig.TASK_SUPPLY_TOPIC)
@Slf4j
public class SupplyTaskHandler implements MsgHandler {
    @Autowired
    private VMService vmService;


    /*
    * 业务分析：
    *   1.接受消息并解析评判断数据
    *     解析后：SupplyContract
    *     判断innerCode、判断补货的List
    *   2.获得最少工单的人（userId）
    *       项目的前缀标识+时间+区域id+运维/运营角色（1002）
    *       通过innerCode查询售货机，从中获得RegionId
    *   3.创建补货工单的业务类：TaskReq
    *       TaskReq:
    *           productType：VmSystem.TASK_TYPE_SUPPLY
    *           innerCode: SupplyContract.innerCode
    *           createType: 0
    *           userId:第二布获得
    *           details:将报文中的SupplyChannel转为TaskDetailsReq
    *   4.调用TaskService中的CreateTask方法
    * */
    @Autowired
    private TaskService taskService;

    @Override
    public void process(String jsonMsg){

        try {
            //1.解析协议内容
            var supplyContract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
            if(supplyContract==null) return;

            //2.找出被指派人
            var vm = vmService.getVMInfo(supplyContract.getInnerCode());
            var userId = taskService.getLeastUser(vm.getRegionId(), false);
            if(vm==null || userId==0) return;
            //3.创建补货工单
            var taskReq=new TaskReq();
            taskReq.setUserId(userId);
            taskReq.setCreateType(0);//创建类型
            taskReq.setProductType(VMSystem.TASK_TYPE_SUPPLY);
            taskReq.setInnerCode(supplyContract.getInnerCode());
            taskReq.setAssignorId(0);//创建人
            taskReq.setDesc("自动补货工单");

            List<SupplyChannel> supplyChannels = supplyContract.getSupplyData();


            var taskDetailsReqs = supplyChannels.stream().map(c -> {
                var taskDetailsViewModel = new TaskDetailsReq();
                taskDetailsViewModel.setChannelCode(c.getChannelId());
                taskDetailsViewModel.setExpectCapacity(c.getCapacity());
                taskDetailsViewModel.setSkuId(c.getSkuId());
                taskDetailsViewModel.setSkuName(c.getSkuName());
                taskDetailsViewModel.setSkuImage(c.getSkuImage());
                return taskDetailsViewModel;
            }).collect(Collectors.toList());


            taskReq.setDetails(taskDetailsReqs);  //补货详情

            taskService.createTask(taskReq);
        } catch (Exception e) {
            e.printStackTrace();
            log.error( "创建自动补货工单出错"+e.getMessage() );
        }
    }
}